from pari import *
from time import clock
x = var("x")
print x
assert str(x)=="x"
print x**2
assert str(x**2)=="x^2"
print x**2
print x+2
assert str(x+2)=="x + 2"
print x-2
assert str(x-2)=="x - 2"
print x/2
assert str(x/2)=="1/2*x"
print x/x
assert str(x/x)=="1"
two = parse("2")
print two
assert str(two)=="2"
print cos(2)
print cos(two)
print I
assert str(I)=="I"
print Pi
print Euler
print bernfrac(6)
print bernvec(6)
print zeta(2)/Pi**2*6
print gamma(5)
print bernfrac(cast(6))
print int(cast(5))
print float(cast(5.))

# Help also works:
print ellinit.__doc__

t=cast(0)
global a
a=0

def call0():
    print "Called"
    pass

print 'Produces exception here:'    
try:
    forstep(1,10,1,call0)
except TypeError:
    print 'Exception occured'

def call1(v):
    print v
forstep(1,10,1,call1)
print(t)
print "forstep succeeded"

def call2(v):
    return v
print sum(0,10,call2)
print intnum(0,1,call2)


def call3(v):
    return sin(v)
    
c1 = clock()
for i in range(0,50):
    intnum(0,1,call3)
print (clock()-c1)/50

c1 = clock()
for i in range(0,50):
    parse("intnum(t=0,1,sin(t))")
print (clock()-c1)/50

t=cast(0.5)

c1 = clock()
for i in range(0,10000):
    y=sin(t)
print (clock()-c1)/10000

c1 = clock()
for i in range(0,10000):
    call3(1)
print (clock()-c1)/10000

c1 = clock()
parse("for(i=1,10000,sin(0.5))")
print (clock()-c1)/10000

v=cast(0)
c1 = clock()
forstep(0,10000,1,call3)
print (clock()-c1)/10000

p=-1

print 'Calling intnum from inside a function'
def mytestfunc(p):
    print "My test function"
    def toint(v):
        val = v+p
        return val
    print intnum(0,1,toint)

mytestfunc(1)
mytestfunc(0)

print 'creating a power series'
print 1/(1-x+O(x**10))
print 'creating a padic number'
print -1+O(2**100)
print 'creating a vector'
v=cast((1,2,3,4,5))
print v
print 'accessing an element'
print v[0],v[-1]
print 'changing an element'
v[2]=55
print v
print 'changing a vector into a column'
print Col(v)
print 'into a polynomial'
print Pol(v)
print 'into a power series'
print Ser(v)
print 'creating a matrix'
m=Mat((Col((1,2)),Col((3,4))))
print m
print 'accessing an element'
print m(0,1)
print m(0)
print 'exception here'
try:
    print m(0,0,0)
except error,e:
    print 'exception caught:', e

gp("x=2")
print eval(x)
kill(x)
print eval(x)

print subst(x**2+x+1,x,1)

real_prec(300)
print exp(2)
print real_prec()
print 1/2
